﻿@using Website.CRUDServices
@{
    this.ViewBag.Title = "Opdatér Nyhedsinformation";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Funds = this.ViewData["Funds"] as List<Fund>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var Types = this.ViewData["Types"] as List<NewsInformationType>;
    var errorMsg = new HtmlString(string.Empty);
    var filesInfo = this.ViewData["FilesInfo"] as List<Website.CRUDServices.File> ?? new List<Website.CRUDServices.File>();
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

    var News = this.Model as NewsInformation;
}

@Html.Partial("~/Views/_Shared/LogView.cshtml", News.ID)

<script type="text/javascript">
    //toggle script
    function toggle(id) {
        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        }
    }

    function reverseToggle(id) {
        if (document.getElementById(id).style.display == 'block') {
            document.getElementById(id).style.display = 'none';
        }
    }

    //show/his script

    function checkType(id) {
        var selectTypeObj = document.getElementById('Type');

        if (selectTypeObj.selectedIndex == -1) {
            return;
        }

        var selectChosenType = selectTypeObj.options[selectTypeObj.selectedIndex].text;

        if (selectChosenType == 'Nyhedsbrev') {
            toggle(id);
        } else {
            if (selectChosenType == 'Formidlings information') {
                toggle(id);
            } else {
                reverseToggle(id);
            }
        }
    }
</script>
@if (News.IsInformationAboutFunds || News.IsInformationAboutProgress || News.IsInformationAboutResults)
{
    <script>
        $(function() {
            toggle('additionals');
        });
    </script>
}
@if (News.IsInformationAboutFunds)
{
    <script>
        $(function() {
            toggle('showFunds');
        });
    </script>
}

<script>
    $(function() {
        $("#datepicker").datepicker();
        $('#Funds').multiSelect();
        $('#OwnerProjects').multiSelect();
    });
</script>

@using (this.Html.BeginForm("UpdateNewsInformation", "NewsInformation", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="error">
        @errorMsg
    </div>

    // *******************Nyhedsinformation*************************

    // hidden field to hold ID of the object
    <input type="hidden" id="ID" name="ID"  readonly="readonly" value="@News.ID" />

    // name
    <div class="formBullet">Navn på nyhedsinformation</div>
    <input type="text" id="Name" name="Name" value="@News.Name"/>

    // date
    <div class="formBullet">Dato</div>
    <input type="text" id="datepicker" name="TimeOfActivity" readonly="readonly" value="@News.TimeOfActivity" />

    // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet</div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects.Where(x => (News.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
            }
            @foreach (Project proj in Projects.Where(x => !(News.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

            // types chooser
    <div class="formBullet">Nyhedstype</div>
    <div class="formEntry">
        <select name="Type" id="Type" onchange=" checkType('additionals') ">
            @foreach (NewsInformationType typ in Types)
            {
                if (News.Type == typ.ID)
                {
                    <option value="@typ.ID" selected="selected">@typ.Name</option>
                }
                else
                {
                    <option value="@typ.ID">@typ.Name</option>
                }
            }
        </select>
    </div>

            // explanation
    <div class="formBullet">Yderligere beskrivelse</div>
    <textarea name="Comments" id="Comments">@News.Comments</textarea>

    <!--filechooser-->
    <div class="formBullet">
        Fil til arkivering<br />
    </div>
    <div class="formEntry">
        @Microsoft.Web.Helpers.FileUpload.GetHtml("Files", 0, true, false, "Tilføj flere filer", "Vælg fil til arkivering")
    </div>
    
    <div class="formBullet">
            Eksisterende filer<br />
        </div>
        <div class="formEntry">
            <script type="text/javascript">
                function openFile(id) {
                    var url = "@Url.Action("Open", "File", new { ID = ".id." })";
                    window.open(url.replace(".id.", id), "blank_" + id);
                }
            </script>
            <table class="fileTable" id="FileTable">
          
              @{
                  var fileIDs = News.Files;

                    foreach (var file in filesInfo.Where(x => fileIDs.Contains(x.ID)))
                    {
                        <tr id="@Html.Raw("tr_" + file.ID)">
                            <td>@file.Name</td>
                            <td>
                                <input type="button" value="Se fil" onclick="@Html.Raw("javascript:openFile('" + file.ID + "');")" />
                            </td>
                            <td><input type="button" value="Slet fil" class="deleteButton" id="@Html.Raw("del_" + file.ID)"/>
                                <script>
                                    $("#@Html.Raw("del_" + file.ID)").click(function () {
                                        $('#OwnerNewsID').val("@News.ID");
                                        $('#FileID').val("@file.ID.ToString()");
                                        $("#deleteDialog").dialog({
                                            width: "340px",
                                            modal: true
                                        });
                                    });
                                </script>

                            </td> 
                        </tr>
                    }
                }
            </table>
        </div>
    
    <!--archive location-->
    <div class="formBullet">Fysisk arkiverings placering</div>
    <input type="text" id="ArchiveLocation" name="ArchiveLocation" value="@News.ArchiveLocation"/>

    <div id="additionals" class="open_hidden">
        <br />
<fieldset class="chosenFields">
    <legend>Information om:</legend>
    <br />
        
        @if (News.IsInformationAboutFunds)
        {
            <label>
                <input type="checkbox" name="informationType" value="funds" onchange=" toggle('showFunds') " checked />Fonde/puljer</label>
        }
        else
        {
            <label>
                <input type="checkbox" name="informationType" value="funds" onchange=" toggle('showFunds') " />Fonde/puljer</label>
        }
        @if (News.IsInformationAboutProgress)
        {
            <label>
                <input type="checkbox" name="informationType" value="progress" checked />Fremdrift</label>
        }
        else
        {
            <label>
                <input type="checkbox" name="informationType" value="progress" />Fremdrift</label>
        }
        @if (News.IsInformationAboutResults)
        {
            <label>
                <input type="checkbox" name="informationType" value="results" checked />Resultater</label>
        }
        else
        {
            <label>
                <input type="checkbox" name="informationType" value="results" />Resultater</label>
        }


        <div id="showFunds" class="open_hidden">
            <!--Funds-->
            <div class="formBullet">Vælg fonde/puljer</div>
            <div class="formEntry">
                <select multiple="multiple" name="Funds" id="Funds">

                    @foreach (Fund fun in Funds.Where(x => (News.Funds ?? new Guid[0]).Contains(x.ID)))
                    {
                        <option value="@fun.ID" selected="selected">@fun.Name</option>
                    }
                    @foreach (Fund fun in Funds.Where(x => !(News.Funds ?? new Guid[0]).Contains(x.ID)))
                    {
                        <option value="@fun.ID">@fun.Name</option>
                    }
                </select>
                <script src="@Url.Content("jquery.multi-select.js")" type="text/javascript"> </script>
            </div>
        </div>
    </fieldset>
    </div>

    <div class="buttonHolder">
        <script type="text/javascript">
            function confirmDelete() {
                if (confirm("Er du sikker på at du vil slette aktiviteten? Dette valg kan ikke fortrydes.")) {
                    location.href = '@this.Url.Action("DeleteNewsInformation", new { ID = News.ID })';
                }
            }
        </script>
            <input type="button" value="Slet" class="deleteButton margin-right-20" 
                 onclick="confirmDelete();"
                />
        <input type="submit" value="Opdatér" />
    </div>
}

<script>

    $(function () {
        $('#DeleteFileForm').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        //var methodTypeID = $('#ClusterMethodTypeID').val();
                        var fileID = $('#FileID').val();

                        $('#tr_' + fileID).remove();


                        $("#deleteDialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#DeleteFileForm').html(result);
                    }
                }
            });
        });
    });
</script>

<div id="deleteDialog" title="Slet fil?" style="display: none;">
@using (this.Html.BeginForm("DeleteNewsInformationFileJson", "NewsInformation", FormMethod.Post, new { id = "DeleteFileForm" }))
{
    <input type="hidden" name="OwnerNewsID" id="OwnerNewsID" value="" />
    <input type="hidden" name="FileID" id="FileID" value="" />
    <p>
        Er du sikker på at du vil slette filen?<br />
        (Denne handling kan ikke fortrydes)
    </p>
    
    <div class="buttonHolder">
        <input type="submit" value="Slet" />
    </div>
}
</div>